package vip.zhenzicheng.concurrent_program.thread.sgg.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author zhenzicheng
 * @date 2022-06-21 20:04
 */
public class CompletableFutureFastDemo {

  public static void main(String[] args) {
    CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> {
      System.out.println("A come in");
      try {
        TimeUnit.SECONDS.sleep(2);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return "playA";
    });

    CompletableFuture<String> futureB = CompletableFuture.supplyAsync(() -> {
      System.out.println("B come in");
      try {
        TimeUnit.SECONDS.sleep(3);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return "playB";
    });

    CompletableFuture<String> result = futureA.applyToEither(futureB, f -> f + " is winner");

    System.out.println(Thread.currentThread().getName() + "\t----" + result.join());
  }
}
